<?php
require_once("functions.php");
session_start();
mysql_select_db($db_baza_date, $baza) or die ('Cannot connect to server! Please retry later!');
set_time_limit (100);
mysql_set_charset('utf8', $baza);

$error = "";

if($_POST['step'] == 1)
{
  $verificare = verificare_formular($_POST);

  //print_r($verificare);
  
  if($verificare["user"]){
    $error .= "<p align=\"center\"> Eroare: Lungimea username-ului trebuie sa fie cuprinsa intre 4 si 20. </p>";
  }
  if($verificare["name"]) {
    $error .= "<p align=\"center\"> Eroare: Lungimea numelui trebuie sa fie cuprinsa intre 4 si 20. </p>";
  }
  if($verificare["passlen"]) {
    $error .= "<p align=\"center\"> Eroare: Lungimea parolei trebuie sa fie cuprinsa intre 4 si 20. </p>";
  }
  if($verificare["pass"]) {
    $error .= "<p align=\"center\"> Eroare: Parolele nu coincid. </p>";
  }
  if($verificare["email"]) {
    $error .= "<p align=\"center\"> Eroare: Formatul adresei de email nu este corect. </p>";
  }
  if($verificare["ok"]) {
    $code = saveDates($_POST);
  }
	if($code == 1)
            {
		$message = "Saved.";
                print '<p align="center">'.$msg.'</p>
       <p align = "center">
       <a href="index.php">Prima pagina/Logare</a>
		</p>
       ';
            }
	elseif ($code == 0)
		$message = "Error to save.";
	elseif ($code == -1)
		$message = "Use other e_mail.";
}

?>
<html>
<head>
<title>Formular de creare cont</title>
<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript">
	function validare()
	{
		if(document.getElementById("user").value.length < 4  || document.getElementById("user").value.length > 20)
		{
			alert("Please insert an username with lenght between 4 and 20.");
			document.getElementById("user").focus();
			document.getElementById("user").select();
			return false;
		}
		var letterNumber = /^[A-Za-z0-9 ]{4,20}$/;
 		if(!document.getElementById("user").value.match(letterNumber))
		{
			alert("Please insert an username with alphanumeric characters.");
			document.getElementById("user").focus();
			document.getElementById("user").select();
			return false;
		}
		if(!document.getElementById("name").value.match(letterNumber))
		{
			alert("Please insert a name with alphanumeric characters.");
			document.getElementById("name").focus();
			document.getElementById("name").select();
			return false;
		}
		if(!document.getElementById("pasw").value.match(letterNumber))
		{
			alert("Please insert a password with alphanumeric characters.");
			document.getElementById("pasw").focus();
			document.getElementById("pasw").select();
			return false;
		}		
		if(document.getElementById("pasw").value !=  document.getElementById("pasw2").value)
		{
			alert("Please write both passwords exactly.");
			document.getElementById("pasw").focus();
			document.getElementById("pasw2").select();
			return false;
		}
		//verificam email
		if(!checkEmail("email"))
		{
			alert("Please insert an email adress.");
			document.getElementById("email").focus();
			document.getElementById("email").select();
			return false;
		}
		return true;
	}
</script>
</head>

<body>
<?php echo $error;?>
<p align=center>Creati-va un cont</p>
<br/>
<form action="<?php print $_SERVER['PHP_SELF']?>" method="POST" onsubmit="return validare()" >
<input type="hidden" name="step" id="step" value="1">
<table width="100%" border="0" cellspacing="5" cellpadding="5">

<tr>
	<td align=right><font size=4 color=blue>Username:</font></td>
	<td><input name="user" id="user" type="text"  size="30" maxlength="20"></>
	<font color="red" size=2>*intre 4 si 20 de caractere alfanumerice</font></td>
</tr>

<tr>
	<td align=right><font size=4 color=blue>Numele dumneavoastra:</font></td>
	<td><input name="name" id="name" type="text"  size="30" maxlength="20"></>
	<font color="red" size=2>*intre 4 si 20 de caractere alfanumerice</font></td>
</tr>

<tr>
	<td align=right><font size=4 color=blue>Parola:</font></td>
	<td><input name="pasw" id="pasw" type="password"  size="30" maxlength="20"> </>
	<font color="red" size=2>*intre 4 si 20 de caractere alfanumerice</font></td>
</tr>

<tr>
	<td align=right><font size=4 color=blue>Rescrieti parola:</font></td>
	<td><input name="pasw2" id="pasw2" type="password"  size="30" maxlength="20"> </></td>
</tr>

<tr>
	<td align=right><font size=4 color=blue>E-mail:</font></td>
	<td><input name="email" id="email" type="text" size="30" maxlength="30"> </>
		<font color="red" size=2>*ex: exemplu@yahoo.com</font>
	</td>
<tr/>
</table>

<p align=center><input type="submit" value="Trimite inregistrarea"></p>
</form>
</body>
</html>